﻿<?php
ini_set("memory_limit", "300M");

if (!empty($_FILES)) {

	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';	
	$tName = $_REQUEST['fileTrueName'];
	$fileTypeArray = explode('.',$_FILES['Filedata']['name']);
	if (count($fileTypeArray)<=1){
		die('文件类型错误！');
	}else{
		$fileTpye = strtolower($fileTypeArray[(count($fileTypeArray)-1)]);
		$tName = $tName.'.'.$fileTpye;
	}
	$targetFile =  strtolower(str_replace('//','/',$targetPath) .$tName);
	logwrite($targetFile);
	if (!file_exists($targetPath)){
		mkdir(str_replace('//','/',$targetPath), 0755, true);
	}

	move_uploaded_file($tempFile,$targetFile);

	//制定缩略图

	

	if ($fileTpye=='jpg' or $fileTpye=='jpeg'){
	    
		$imgSrc = imagecreatefromjpeg($targetFile); 

	}
	
	if($fileTpye=='gif'){

		$imgSrc = imagecreatefromgif($targetFile);

	}

	if($fileTpye=='bmp'){

		$imgSrc = imagecreatefromwbmp($targetFile);

	}

	if($fileTpye=='png'){

		$imgSrc = imagecreatefrompng($targetFile);

	}

	$imgWidth = imagesx($imgSrc); 

	$imgHeight = imagesy($imgSrc);

	if($imgWidth>=500)	{

		$beishu = ceil($imgWidth/450);

		$x = ceil($imgWidth/$beishu); 

		$y = ceil($imgHeight/$beishu); 

	}else{

		$x = $imgWidth;

		$y = $imgHeight; 

	}					

	$dst = ImageCreateTrueColor($x,$y); 

	ImageCopyResampled($dst,$imgSrc,0,0,0,0,$x,$y,$imgWidth,$imgHeight); 

	//unlink($targetFile);

	$thumbFile = $targetFile.'.thumb.'.$fileTpye;
	logwrite($thumbFile);
	if ($fileTpye=='jpg' or $fileTpye=='jpeg'){	    
		imagejpeg($dst,$thumbFile);	
	}
	if($fileTpye=='gif'){
		imagegif($dst,$thumbFile);	
	}
	if($fileTpye=='bmp'){
		imagewbmp($dst,$thumbFile);	
	}

	if($fileTpye=='png'){
		imagepng($dst,$thumbFile);	
	}	
	imagedestroy($dst);
	logwrite(str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile));
	echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}

function logwrite($data){
	file_put_contents('a.txt', date("Y-m-d H:i:s").' '.$data."\r",FILE_APPEND);
}

?>